package com.itboye.jmsfhz.app.login.ui;

import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.ValidateImageView;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.ui.main.MainPresenter;
import com.itboye.jmsfhz.data.presenter.GetYanzhengMaPresenter;
import com.itboye.jmsfhz.main.MainActivity;

@SuppressLint("ResourceAsColor")
public class RegiterActivity extends BaseActivity implements OnClickListener,
		Observer {

	private ValidateImageView view = null;
	String[] responseArray = null;

	EditText register_input_phonenum, register_test_password,
			register_input_password, register_ok_input_password;
	Button register_now;
	Button getYzm;
	private String userName;
	private String pwd, numPwd;;
	MainPresenter mainPresenter;
	TimeCount count;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	private String phoneNumber;
	private String type;
	Button login_login_back;

	private ImageView registimgok;
	private TextView txtregistQiYe;
	Button btnyanzhengma;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_regist;
	}

	@Override
	public void onMyCreate() {
		mainPresenter = new MainPresenter(this);
		registimgok.setBackgroundResource(R.drawable.ok);
		txtregistQiYe.setTextColor(getResources().getColor(R.color.main_color));
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);

		LinearLayout linear = (LinearLayout) findViewById(R.id.btnyanzhengma);
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
		view = new ValidateImageView(this);
		linear.addView(view);
		// responseArray = view.getValidataAndSetImage(getRandomInteger());

		// Toast.makeText(this, getResponseStr(responseArray), 2000).show();
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				phoneNumber = register_input_phonenum.getText().toString();
				if (!isEmpty(phoneNumber)) {

					getYanzhengMaPresenter.getVerificationCode(phoneNumber,
							1 + "");
				} else {
					ByAlert.alert("请输入用户名");
				}
			}
		});

	}

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.register_now:
			userName = register_input_phonenum.getText().toString().trim();
			pwd = register_input_password.getText().toString().trim();
			numPwd = register_ok_input_password.getText().toString().trim();
			pwd = numPwd;
			if (isEmpty(userName)) {
				ByAlert.alert("用户名为空");
			}
			if (isEmpty(pwd)) {
				ByAlert.alert("密码不能为空");
			}
			if (isEmpty(numPwd)) {
				ByAlert.alert("密码不能为空");
			}
			if (isEmpty(type)) {
				ByAlert.alert("验证码为空");
			} else {
				mainPresenter.register(userName, pwd, type);
			}
			break;
		case R.id.btnyanzhengma:
			phoneNumber = register_input_phonenum.getText().toString();
			if (!isEmpty(phoneNumber)) {
				getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
				getYanzhengMaPresenter.getVerificationCode(phoneNumber, 1 + "");
			} else {
				ByAlert.alert("请输入用户名");
			}
			break;
		case R.id.login_login_back:
			finish();
			break;
		case R.id.registimgok:
			registimgok.setBackgroundResource(R.drawable.ok);
			txtregistQiYe.setTextColor(getResources().getColor(
					R.color.main_color));
			break;
		case R.id.txtregistQiYe:
			registimgok.setBackgroundResource(R.drawable.ok);
			txtregistQiYe.setTextColor(getResources().getColor(
					R.color.main_color));
			break;
		default:
			break;
		}
	}

	public boolean isEmpty(String msg) {
		if (msg == null || msg.equals(""))
			return true;
		return false;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(arg1);
		if (entity != null) {
			if (entity.getEventType().equals(
					GetYanzhengMaPresenter.YanzhengmaSuccessEvent)) {
				type = entity.getData().toString();
				Log.e("type", type);
				responseArray = view.getValidataAndSetImage(getRandomInteger());
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < responseArray.length; i++) {
					sb.append(responseArray[i]);
				}

				// register_test_password.setText(sb+"");
				// register_test_password.setText(type);
				// getResponseStr(type);
			} else if (entity.getEventType().equals(
					GetYanzhengMaPresenter.YanzhengmaFailEvent)) {
				ByAlert.alert(""+entity.getData());
			} else if (entity.getEventType().equals(
					MainPresenter.RegisterSuccessEvent)) {
				ByAlert.alert("注册成功");
				finish();
			} else if (entity.getEventType() == MainPresenter.RegisterFailureEvent) {
				 Toast.makeText(this, entity.getData().toString(), 0).show();
//				ByAlert.alert("注册成功");
			}
		} else {
			ByAlert.alert("请求失败！");
		}
	}

	// 为数组赋值1~9的随机数
	private String[] getRandomInteger() {
		String[] reuestArray = new String[4];
		Log.e("!!!1", type);
		for (int i = 0; i < 4; i++) {
			reuestArray[i] = String.valueOf(type).charAt(i) + "";
			Log.e("!!!2", reuestArray[i]);
		}
		for (int i = 0; i < 4; i++) {
			Log.e("!!!", reuestArray[i]);
		}
		return reuestArray;
	}

	// 获取返回的数组
	private String getResponseStr(String[] response) {
		StringBuffer stringBuffer = new StringBuffer();
		for (String type : response) {
			stringBuffer.append(type);
		}
		return stringBuffer.toString();
	}

}
